<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.art_id"
          :title="item.title"
          @click="toarticle(item.art_id)"
        >
          <template #label>
            <!-- 主体内容区域 -->
            <van-grid :border="false" :column-num="item.cover.type">
              <!-- 列表项 -->
              <van-grid-item
                v-for="(it, index) in item.cover.images"
                :key="index"
              >
                <van-image fit="cover" :src="it" lazy-load />
              </van-grid-item>
            </van-grid>
            <!-- 底部额外信息区域 -->
            <div class="meta">
              <!-- 左侧 -->
              <div class="info-box">
                <span>{{ item.aut_name }}</span>
                <span>{{ item.comm_count }}评论</span>
                <span>{{ item.pubdate | times }}</span>
              </div>
              <!-- 右侧 -->
              <van-icon name="cross"></van-icon>
            </div>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
// import axios from 'axios'
// import 变量名（一般写子组件名） from '其他子组件路径'
import { getarticles } from '../../api/home'
export default {
  // components:{
  //  变量名
  // },
  name: 'articleList',
  props: ['channelid'],
  created () {
    console.log(this.channelid)
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      timestamp: undefined
    }
  },
  methods: {
    toarticle (id) {
      this.$router.push({ name: 'article', params: { id } })
    },
    async onLoad () {
      if (this.timestamp === undefined) {
        this.timestamp = Date.now()
      }

      const res = await getarticles(this.channelid, this.timestamp)
      setTimeout(() => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        console.log(res)
        this.list.push(...res.data.data.results)
        this.timestamp = res.data.data.pre_timestamp
        this.loading = false

        if (this.list.length >= 40) {
          this.finished = true
        }
      }, 1000)
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    }
  }
}
</script>
<style></style>
